package com.bw.zg3park2212.order

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.OrderAdpter
import com.bw.zg3park2212.databinding.ActivityOrderBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path ="/app/OrderActivity")
class OrderActivity : BaseActivity<ActivityOrderBinding,OrderViewModel>() {
    lateinit var orderAdpter: OrderAdpter
    var types=""
    override fun intitView() {
        orderAdpter= OrderAdpter()
        binding.rv.layoutManager=LinearLayoutManager(this)
        binding.rv.adapter=orderAdpter
        var arr= mutableListOf("全部","新建","进行中","已完成","已转派")
        var typeAdpter= ArrayAdapter(this,android.R.layout.simple_list_item_1,arr)
        binding.patrolStatus.adapter=typeAdpter
        typeAdpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.patrolStatus.onItemSelectedListener= object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                types=arr[position]
                if (types=="全部"){
                    viewmodel.getorder()
                }else{
                    viewmodel.getorderstatus(types)
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
        binding.sv.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (types!="全部"){
                    viewmodel.getordername(query!!,types)
                }else{
                    ToastUtils.showLong("请选择状态")
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                viewmodel.getorder()
                return false
            }

        })
        orderAdpter.addChildClickViewIds(R.id.check_order_xiang,R.id.up_order)
        orderAdpter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.check_order_xiang->
                {
                    ARouter.getInstance().build("/app/OrderXingActivity")
                        .withSerializable("order", orderAdpter.data[position])
                        .navigation()
                }
                R.id.up_order->{
                    ARouter.getInstance().build("/app/OrderUpActivity")
                        .withSerializable("orderup", orderAdpter.data[position])
                        .navigation()
                }
            }
        }

        orderAdpter.setOnItemLongClickListener { adapter, view, position ->
            viewmodel.getorderdel(orderAdpter.data[position].id)
            orderAdpter.remove(position)
            return@setOnItemLongClickListener true
        }
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/OrderAddActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.orderSucsse.observe(this){
            orderAdpter.data.clear()
            orderAdpter.addData(it)
            orderAdpter.notifyDataSetChanged()
        }
        viewmodel.orderstatusSuccse.observe(this){
            orderAdpter.data.clear()
            orderAdpter.addData(it)
            orderAdpter.notifyDataSetChanged()
        }
        viewmodel.ordernameSuccse.observe(this){
            orderAdpter.data.clear()
            orderAdpter.addData(it)
            orderAdpter.notifyDataSetChanged()
            ToastUtils.showLong("查询成功")
        }
        viewmodel.orderdelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getorder()
    }

}